
/*
Coded 5/11/24 - Questions to terence.wood@anu.edu.au

This runs the standard tests of potential issues in difference in difference
analysis for the paper "Does independent review reduce the reported performance 
of aid projects?" 

Coded in Version 18.0 SE.
If you are using an old version of Stata you may need to install estout/esttab
as well as margins/marginsplot.

The code creates: 
1. A chart comparing ongoing and completed project scores.
2. A table of 2 by 2 difference in difference results.
3. Charts of effects over time.
4. A chart of changed results for Papua New Guinea (PNG).
*/


********************************************************************************
********************************Preliminaries***********************************
********************************************************************************

// Note that if you wish to run individual results you can uncomment
// cd and use code below.
// Similarly the datafile is opened three times below.

cd "$data_loc"  // Change directory to location where data are stored.
use "$data_file", clear // Open data
cd "$res_loc" // Change directory to location where analysis will occur. 

graph drop _all

********************************************************************************
********************************Results*****************************************
********************************************************************************

************Figure 3 - Basic chart of project performance trends****************

// Chart showing mean ee by report type (ongoing or final) by year
preserve
collapse (mean) ee, by(year finalreport)

twoway ///
	(line ee year if finalreport==0, lcolor(gs2) lpattern(dash)) ///
	(line ee year if finalreport==1, lcolor(gs2)), ///
	xline(2014 2015 2016 2017 2018, lcolor(gs14) lwidth(1.5cm) lpattern(solid)) ///
	yscale(range(3 5)) ylab(3(1)5) graphregion(color(white)) xscale(range(2013.5 2022)) ///
	legend(order(1 "Ongoing" 2 "Final") position(6) region(lcolor(white))) ///
	ytitle("Mean project performance") 

graph export "6 Figure 3 Trends in report appraisals.jpg", width(10000) replace

graph drop _all

restore
estimates clear		
	
**************Table 4 - Table of difference in difference results***************
preserve

// Project attribute controls to include in some regressions.
local projectcontrols "duration lnbudget i.pacific i.covid"

encode country, gen(cencode) // encoding country so it can be used for fixed effects.

// Basic models	
// Model 1
qui reg ee i.finalreport##i.post2018, vce(cluster pnencode)
estimates store tbt

// Model 2
qui reg ee i.finalreport##i.post2018 `projectcontrols', vce(cluster pnencode)
estimates store tbtcont	

// Sector FE
// Model 3
qui reg ee i.finalreport##i.post2018 `projectcontrols' i.sector, vce(cluster pnencode)
estimates store tbtcontsfe	

// Country FE
// Model 4
qui reg ee i.finalreport##i.post2018 `projectcontrols' i.cencode, vce(cluster pnencode)
estimates store tbtcontcfe

// Country and sector FE
// Model 5
qui reg ee i.finalreport##i.post2018 `projectcontrols' i.sector i.cencode, vce(cluster pnencode)
estimates store tbtcontcsfe	
	
// Project Project FE
// Model 6
qui areg ee i.year i.treated if any_issue==0 & humanitarian==0, ///
	absorb(pnencode) vce(cluster pnencode)
estimates store pf

// Results to table
esttab tbt tbtcont tbtcontsfe tbtcontcfe tbtcontcsfe pf ///
	using "7 Table 4 Results DiD.rtf", replace ///
	b(2) se(2) star(* 0.1 ** 0.05 *** 0.01) ///
	nobase nogap nodep noconst label nomtitle eqlabels(none) varwidth(15) modelwidth(6) ///
	indicate("Sector FE = *sector" "Country FE = *cencode" "Project FE = *year" ///
	"Project Controls = duration lnbudget *pacific *covid") /// 
	rename(1.finalreport#1.post2018 "Diff in Diff" r1vs0.treated "Diff in Diff" ///
	1.treated "Diff in Diff") order("Diff in Diff") ///
	note(The dependent variable in all models is assessed project performance. ///
	Performance scores range between 1 and 6. Robust standard errors are clustered ///
	at the project level in all models. The coefficient "Diff in Diff" captures ///
	the change in differences between appraisals of ongoing projects and appraisals of ///
	completed projects that occurred when independent review was introduced. ///
	The control variables included in Model 2 are the natural log of project ///
	size, project duration, whether the project appraisal occurred during ///
	COVID-19 and whether the project was in the Pacific or not. ///
	Model 3 contains country fixed effects. Model 4 contains sector fixed effects. ///
	Model 5 contains country and sector fixed effects. Model 6 contains project ///
	fixed effects - its sample size is smaller because some projects could not ///
	be tracked over time.) fonttbl(\f0\fnil Calibri; )
	
estimates clear	

	
*****************Figures 4 & 5 - Charts of effects over time********************

// Project controls to include in some charts.
local projectcontrols "duration lnbudget i.pacific"

// Preliminaries
graph drop _all
set graph off

//// Figure 4
// Chart with no project controls.
qui reg ee i.finalreport##ib2018.year, vce(cluster pnencode)  

qui margins, dydx(finalreport) at (year=(2014(1)2022))
marginsplot, ///
	plotopts(connect(none) xlab(, nogrid) ///
	xscale(range(2013.75 2022)) xline(2014 2015 2016 2017 2018, ///
	lcolor(gs15) lwidth(vvvthick) lpattern(solid)) yline(0, lcolor(*.3)) ///
	msymbol(D) mcolor(black) mlabcolor(black) mfcolor(white) /// 
	mlabel(_margin) mlabformat(%9.2f) mlabpos(12) mlabgap(*2)) ///
	ciopts(lwidth(*3) lcolor(gs12) msize(vtiny)) ///
	ytitle("Performance difference: ongoing - final") title(No controls) ///
	name(etnocont) 

// Chart with project controls.	
qui reg ee i.finalreport##ib2018.year `projectcontrols', vce(cluster pnencode) 

qui margins, dydx(finalreport) at (year=(2014(1)2022))  
marginsplot, ///
	plotopts(connect(none) xlab(, nogrid) ///
	xscale(range(2013.75 2022)) xline(2014 2015 2016 2017 2018, ///
	lcolor(gs15) lwidth(vvvthick) lpattern(solid)) yline(0, lcolor(*.3)) ///
	msymbol(D) mcolor(black) mlabcolor(black) mfcolor(white) /// 
	mlabel(_margin) mlabformat(%9.2f) mlabpos(12) mlabgap(*2)) ///
	ciopts(lwidth(*3) lcolor(gs12) msize(vtiny)) ///
	ytitle("Performance difference: ongoing - final") title(Project controls) ///
	name(etcont)

// Combine two charts.
graph combine etnocont etcont, cols(1) graphregion(color(white))

// Display charts.
set graph on
graph display, ysize(12) xsize(8)
graph export "8 Figure 4 Effects over time.jpg", width(10000) replace

//// Figure 5
//Project Fixed effects

drop if any_issue==1   // Drop projects with issues as these are excluded from 
					   // project FE models.

xtset pnencode year

qui xtreg ee i.pnencode i.finalreport##ib2018.year /// 
	if any_issue==0 & humanitarian==0 , fe vce(cluster pnencode)

qui margins, dydx(finalreport) at (year=(2015(1)2022)) noestimcheck

marginsplot, ///
	plotopts(connect(none) xlab(, nogrid) ///
	xscale(range(2014.75 2022)) xline(2015 2016 2017 2018, ///
	lcolor(gs15) lwidth(vvvthick) lpattern(solid)) yline(0, lcolor(*.3)) ///
	msymbol(D) mcolor(black) mlabcolor(black) mfcolor(white) /// 
	mlabel(_margin) mlabformat(%9.2f) mlabpos(12) mlabgap(*2)) ///
	ciopts(lwidth(*3) lcolor(gs12) msize(vtiny)) ///
	ytitle("Performance difference: ongoing - final") title("")

graph export "9 Figure 5 Project FE Event study.jpg", width(10000) replace	
	
restore
estimates clear
graph drop _all


******************Figure 6 - Change in PNG's ranking chart**********************


// Preliminaries
graph drop _all
set graph off

// Create chart of projects without reviewed performance appraisals.
preserve

drop if humanitarian==1   // Get rid of humanitarian emergency projects,
						  // which are not included in any analysis in the paper.

// Remove externally reviewed appraisals.
drop if treated==1        

collapse (mean) ee, by(country)  // collapse data to get means by country

gen png=0  // create a PNG dummy
replace png=1 if country=="Papua New Guinea"
label define pnglab 0 "Elsewhere" 1 "PNG"
label values png pnglab
label var png "Papua New Guinea"

gsort -ee  // sort countries from highest scores to lowest
gen order = _n  // generate a variable that reflects highest-lowest order.
                // this will be used to put PNG's label in the right place.
gsort -png  // sort papua new guinea so it is first 
local place = order[1]  // Capture PNG's order from the variable order and 
				        // put it in a macro. 

twoway /// chart
	(bar ee order if png==0, fcolor(gs14) lcolor(gs2)) ///
	(bar ee order if png==1, fcolor(gs2) lcolor(gs2)) ///
	, legend(off) xlab(`place' "PNG", notick nogrid)  /// 
	yscale(range(2(1)5)) ylab(2(1)5, nogrid) ///
	xtitle("") ytitle("Average performance") ///
	title(Untreated, size(medium)) name(untreated)
	
restore

// Create chart of projects with project appraisals that were externally reviewed.
preserve

drop if humanitarian==1

// Remove appraisals that were not externally reviewed.
drop if treated==0  

collapse (mean) ee, by(country)

gen png=0  // create a PNG dummy.
replace png=1 if country=="Papua New Guinea"
label define pnglab 0 "Elsewhere" 1 "PNG"
label values png pnglab
label var png "Papua New Guinea"

gsort -ee
gen order = _n
gsort -png
local place = order[1]

twoway ///
	(bar ee order if png==0, fcolor(gs14) lcolor(gs2)) ///
	(bar ee order if png==1, fcolor(gs2) lcolor(gs2)) ///
	, legend(off) xlab(`place' "PNG", notick nogrid) ///
	yscale(range(2(1)5)) ylab(2(1)5, nogrid) ///
	xtitle("Country") ytitle("Average performance") ///
	title(Treated, size(medium)) name(treated)
	
// Combine two charts
graph combine untreated treated, cols(1)  

set graph on

graph display, ysize(6) xsize(4) margins(small) 
graph export "10 Figure 6 Changes to Papua New Guinea.jpg", width(10000) replace
graph drop _all

restore